package com.monna.games_2048;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.monna.listener.IGamesListener;
import com.monna.service.MusicService;
import com.monna.util.ScoreUtil;
import com.monna.view.GamesLayout;

/**
 * 2048游戏功能、重新开始功能、撤销功能、排行榜功能
 *
 * @author 廖梦青
 */
public class MainActivity extends AppCompatActivity implements IGamesListener {

    private GamesLayout gamesLayoutView;
    private TextView score;
    private TextView highestScore;
    private int maxScore = 0;
    private String account;
    private Button gamesHelp;
    private Button leaderboard;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = getIntent();
        String isBgMusic = intent.getStringExtra("isBgMusic");
        System.out.println("isBgMusic=" + isBgMusic);
        /**
         * 管理背景音乐
         */
        bgMusic(isBgMusic);

        /**
         * 初始化用户最高分数
         */
        highestScore = findViewById(R.id.id_highest_score);
        initHeightScore();

        /**
         * 2048游戏功能
         */
        games();

        /**
         * 获取实时分数
         */
        score = findViewById(R.id.id_scope);

        /**
         * 重新开始
         */
        Button reStart = findViewById(R.id.start);
        reStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reStartGame();
            }
        });

        /**
         * 撤销
         */
        gamesHelp = findViewById(R.id.games_help);
        gamesHelp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, GamesHelpActivity.class);
                startActivity(intent);
            }
        });

        /**
         * 排行榜
         */
        leaderboard = findViewById(R.id.leaderboard);
        leaderboard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, LeadercoardActivity.class);
                startActivity(intent);
            }
        });
    }

    /**
     * 2048游戏功能
     */
    public void games() {
        gamesLayoutView = findViewById(R.id.id_games);
        gamesLayoutView.setOnGameListener(this);
    }

    @Override
    public void onScoreChange(int s) {
        if (s > maxScore){
            maxScore = s;
            new ScoreUtil(this).userHeightScore(account, maxScore);
        }
        score.setText("" + s);
        highestScore.setText("" + maxScore);
    }

    @Override
    public void onGameOver() {
        new AlertDialog.Builder(this).setTitle("游戏结束").setIcon(R.mipmap.ic_start2048).setMessage("本轮得分：" + score.getText()).setPositiveButton("重新开始", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                activityReStart();
            }
        }).show();
    }

    /**
     * 游戏重新开始
     */
    public void reStartGame() {
        new AlertDialog.Builder(this).setTitle("重新开始")
                .setIcon(R.mipmap.ic_start2048)
                .setMessage("您确定放弃当前游戏，开启新游戏吗？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                activityReStart();
            }
        })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
        }).show();
    }

    /**
     * 重启Activity
     */
    public void activityReStart() {
        Intent intent = getIntent();
        finish();
        startActivity(intent);
    }

    /**
     * 初始化用户最高分
     */
    private void initHeightScore(){
        SharedPreferences preferences = getSharedPreferences("userInfo", this.MODE_PRIVATE);
        account = preferences.getString("account", "");
        maxScore = new ScoreUtil(this).getUserHeightScore(account);
        highestScore.setText("" + maxScore);
    }

    /**
     * 背景音乐
     */
    public void bgMusic(String isBgMusic){
        if (isBgMusic.equals("true")){
            //启动服务，播放背景音乐
            startService(new Intent(MainActivity.this, MusicService.class));
        }
        if (isBgMusic.equals("false")){
            //停止服务，停止播放背景音乐
            stopService(new Intent(MainActivity.this, MusicService.class));
        }
    }
}
